Telegram Group & Telegram Channel
Функция itertools.cycle

В пайтон есть классный модуль itertools для создания собственных итераторов. Функции данного модуля довольно эффективны в работе, поэтому их часто используют в реальных проектах.

Сегодня мы бы хотели показать вам функцию cycle() из itertools. Данная функция принимает на вход итерируемый объект и создает бесконечный итератор, циклически возвращающий элементы данного объекта. Фишка заключается в том, что когда элементы последовательности заканчиваются, итерация начинается вновь с первого элементы.

К примеру, функция cycle() из последовательности ['red', 'white', 'blue'] генерирует повторяющуюся бесконечную. Но важно при проходе при итерации по такому итератору предусмотреть выход из цикла (а не как у нас в первом случае с colors:). Так как это итератор, то мы можем использовать его для получения значений через функцию next(colors).

Мы также можем воспользоваться islice(), который вернет итератор по подмножеству переданного объекта.

#генераторы #itertools



tg-me.com/python_academy/221
Create:
Last Update:

Функция itertools.cycle

В пайтон есть классный модуль itertools для создания собственных итераторов. Функции данного модуля довольно эффективны в работе, поэтому их часто используют в реальных проектах.

Сегодня мы бы хотели показать вам функцию cycle() из itertools. Данная функция принимает на вход итерируемый объект и создает бесконечный итератор, циклически возвращающий элементы данного объекта. Фишка заключается в том, что когда элементы последовательности заканчиваются, итерация начинается вновь с первого элементы.

К примеру, функция cycle() из последовательности ['red', 'white', 'blue'] генерирует повторяющуюся бесконечную. Но важно при проходе при итерации по такому итератору предусмотреть выход из цикла (а не как у нас в первом случае с colors:). Так как это итератор, то мы можем использовать его для получения значений через функцию next(colors).

Мы также можем воспользоваться islice(), который вернет итератор по подмножеству переданного объекта.

#генераторы #itertools

BY Python Academy




Share with your friend now:
tg-me.com/python_academy/221

View MORE
Open in Telegram


Python Academy Telegram | DID YOU KNOW?

Date: |

How Does Bitcoin Work?

Bitcoin is built on a distributed digital record called a blockchain. As the name implies, blockchain is a linked body of data, made up of units called blocks that contain information about each and every transaction, including date and time, total value, buyer and seller, and a unique identifying code for each exchange. Entries are strung together in chronological order, creating a digital chain of blocks. “Once a block is added to the blockchain, it becomes accessible to anyone who wishes to view it, acting as a public ledger of cryptocurrency transactions,” says Stacey Harris, consultant for Pelicoin, a network of cryptocurrency ATMs. Blockchain is decentralized, which means it’s not controlled by any one organization. “It’s like a Google Doc that anyone can work on,” says Buchi Okoro, CEO and co-founder of African cryptocurrency exchange Quidax. “Nobody owns it, but anyone who has a link can contribute to it. And as different people update it, your copy also gets updated.”

The global forecast for the Asian markets is murky following recent volatility, with crude oil prices providing support in what has been an otherwise tough month. The European markets were down and the U.S. bourses were mixed and flat and the Asian markets figure to split the difference.The TSE finished modestly lower on Friday following losses from the financial shares and property stocks.For the day, the index sank 15.09 points or 0.49 percent to finish at 3,061.35 after trading between 3,057.84 and 3,089.78. Volume was 1.39 billion shares worth 1.30 billion Singapore dollars. There were 285 decliners and 184 gainers.

Python Academy from ru


Telegram Python Academy
FROM USA